package com.junzhiit.nengji.cms.web.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.cms.service.GoodsSkuService;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.GoodsSkuContentVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.model.GoodsSkuItems;
import com.junzhiit.nengji.model.SkuContent;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/goodssku")
public class GoodsSkuController extends BaseController {

	@Autowired
	private GoodsSkuService goodsSkuService;

	@ApiOperation(value = "获取商户管理SKU名称及规格")
	@RequestMapping(method = RequestMethod.GET)
	public ResultVO querySkus(String sessionId) {
		return goodsSkuService.queryGoodsSku(sessionId);
	}

	@ApiOperation(value = "获取商品相关sku_items")
	@RequestMapping(value = "/skuitems/{goodsId}", method = RequestMethod.GET)
	public ResultVO queryGoodsSkuItems(@PathVariable String goodsId) {
		return goodsSkuService.queryGoodsSkuItems(goodsId);
	}
	
	@ApiOperation(value = "获取商品相关SKU名称和规格")
	@RequestMapping(value = "/skuitems/bo/{goodsId}", method = RequestMethod.GET)
	public ResultVO queryGoodsSkuItemsBO(@PathVariable String goodsId){
		return goodsSkuService.queryGoodsSkuItemsBO(goodsId);
	}
	
	@ApiOperation(value = "获取商品规格组合内容")
	@RequestMapping(value = "/skucontent/{goodsId}", method = RequestMethod.GET)
	public ResultVO querySkuContent(@PathVariable String goodsId) {
		return goodsSkuService.querySkuContent(goodsId);
	}

	@ApiOperation(value = "保存商品规格组合")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO add(GoodsSkuContentVO goodsSkuContentVO) {
		String goodsSkuItemses = goodsSkuContentVO.getGoodsSkuItemses();
		String skuContents = goodsSkuContentVO.getSkuContents();
		String goodsId = goodsSkuContentVO.getGoodsId();

		List<GoodsSkuItems> goodsSkuItemlist = JsonUtil.toListObject(goodsSkuItemses, GoodsSkuItems.class);
		List<SkuContent> skuContentlist = JsonUtil.toListObject(skuContents, SkuContent.class);

		return goodsSkuService.addGoodsSku(skuContentlist, goodsSkuItemlist, goodsId);
	}

	@ApiOperation(value = "查询商品相关skuContent")
	@RequestMapping(value = "/goodsid", method = RequestMethod.GET)
	public Map<String, Object> findAllByGoodsId(String goodsId, DataTablesParamVO dtp) {
		return goodsSkuService.findAllByGoodsId(goodsId, dtp);
	}

	@ApiOperation(value="更新商品图片")
	@RequestMapping(value="/image",method=RequestMethod.PUT)
	public ResultVO updateGoodsSkuItemsImage(String imageUrl){
		AssertUtil.notBlank(imageUrl, "imageUrl is required");
		return goodsSkuService.updateGoodsSkuItemsImage(imageUrl);
	}
	
}
